﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class Frmbjxxcx : Form
    {
        public Frmbjxxcx()
        {
            InitializeComponent();
        }
        public Frmbjxxcx(string flag)
        {
            InitializeComponent();
            if (flag == "mod")
            {//修改按钮显示
                this.btnMod.Visible = true;
            }
            else if (flag == "del")
            {//删除按钮显示
                this.btnDel.Visible = true;
            }
        }
        public void bindDataGird()//数据绑定
        {
            string classId = this.txtClass.Text.Trim().ToString();
            string zyId = (string)this.combZy.SelectedItem;
            ClassInfoData data = new ClassInfoData();
            data.Classid = classId;
            data.Specialtyid = zyId;
            try
            {
                DataSet ds = ClassInfoOperation.getClassInfo(data);
                this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindDataGird();
        }

        private void Frmbjxxcx_Load(object sender, EventArgs e)
        {
            string ConnectionSttring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection cn = new SqlConnection(ConnectionSttring);
            cn.Open();
            string str = "select 专业id from 班级信息表 group by 专业id";
            SqlCommand cm = new SqlCommand(str, cn);
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.combZy.Items.Add(dr[0]);
            }
            cm.Dispose();
            cn.Close();
            cn.Dispose();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {
                MessageBox.Show("请选择要删除的记录！", "提示");
                return;
            }
            else
            {
                if(MessageBox.Show("确定要删除吗？","删除",MessageBoxButtons.YesNo)==DialogResult.Yes)
                {
                    string classId = this.txtClass.Text.Trim().ToString();
                    string zyId = (string)this.combZy.SelectedItem;
                    ClassInfoData data = new ClassInfoData();
                    data.Classid = classId;
                    data.Specialtyid = zyId;
                    DataSet ds = ClassInfoOperation.getClassInfo(data);
                    this.dataGrid1.DataSource = ds.Tables[0];
                    string id = ds.Tables[0].Rows[index]["班级名称"].ToString();
                    try
                    {
                        if (ClassInfoOperation.deleteClassInfo(id))
                        {
                            MessageBox.Show("删除成功！", "提示");
                            bindDataGird();
                        }
                        else
                        {
                            MessageBox.Show("删除失败!","错误");
                        }
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！","错误");
                    }
                }
            }
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            string classId = this.txtClass.Text.Trim().ToString();
            string zyId = (string)this.combZy.SelectedItem;
            ClassInfoData data = new ClassInfoData();
            data.Classid = classId;
            data.Specialtyid = zyId;
            DataSet ds = ClassInfoOperation.getClassInfo(data);
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            else
            {
                Frmaddclass modclass = new Frmaddclass(ds.Tables[0].Rows[index]["班级名称"].ToString());
                modclass.ShowDialog(this);
            }
        }
    }
    public class ClassInfoData
    {
        private string classid = "";
        private string specialtyid = "";
        private string studentnumber = "";
        private string remark = "";
        public string Classid//班级名称
        {
            get { return classid; }
            set { classid = value; }
        }
        public string Specialtyid//专业代号
        {
            get { return specialtyid; }
            set { specialtyid = value; }
        }
        public string Studentnumber//班级人数
        {
            get { return studentnumber; }
            set { studentnumber = value; }
        }
        public string Remark//备注
        {
            get { return remark; }
            set { remark = value; }
        }
    }
    //ClassInfoOperation
    public class ClassInfoOperation
    {
        private static DataAccess dataAccess = new DataAccess();
        public static bool insertClassInfo(ClassInfoData classInfoData)
        {//插入数据
            string sql = String.Format("insert into 班级信息表(班级名称,专业id,学生人数,备注) values ('{0}','{1}','{2}','{3}')",
                classInfoData.Classid, classInfoData.Specialtyid, classInfoData.Studentnumber, classInfoData.Remark);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updateClassInfo(ClassInfoData classInfoData)
        {//修改数据
            string sql = String.Format("update 班级信息表 set 专业id='{1}',学生人数='{2}',备注='{3}' where 班级名称='{0}'",
                classInfoData.Classid, classInfoData.Specialtyid, classInfoData.Studentnumber, classInfoData.Remark);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool deleteClassInfo(string classId)
        {//删除数据
            string sql = String.Format("delete 班级信息表 where 班级名称='{0}'", classId);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static DataSet getClassInfo(ClassInfoData classInfoData)
        {//获取数据
            string condition = "";//查询条件
            if (classInfoData.Classid != null && classInfoData.Classid != "")
            {
                condition += " and 班级名称='" + classInfoData.Classid + "'";
            }
            if (classInfoData.Specialtyid != null &&! classInfoData.Specialtyid.Equals(""))
            {
                condition += " and a.专业id='" + classInfoData.Specialtyid + "'";

            }
            if (classInfoData.Studentnumber != null && classInfoData.Studentnumber != "")
            {
                condition += " and 学生人数='" + classInfoData.Studentnumber + "'";

            }
            if (classInfoData.Remark != null && classInfoData.Remark != "")
            {
                condition += " and 备注 like '%" + classInfoData.Remark + "%'";
            }
            string sql = "select b.班级名称,a.专业名称,b.学生人数,b.备注 from 专业信息表 a,班级信息表 b where a.专业id=b.专业id" + condition;
            return dataAccess.GetDataSet(sql, "班级信息表");//执行sql语句
        }

    }
}